﻿using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using iShoppingBUS;
using iShoppingDTO;

namespace iShopping.Admin.UserControl
{
    public partial class CapNhapTaiKhoan : System.Web.UI.UserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                FillTaiKhoan();
            }
        }
        private void FillTaiKhoan()
        {

            DataTable dt = TaiKhoanBUS.SelectAllTaiKhoan();
            gvTaiKhoan.DataSource = dt;
            gvTaiKhoan.DataBind();
        }

        protected void gvTaiKhoan_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            // phan trang
            gvTaiKhoan.PageIndex = e.NewPageIndex;
            FillTaiKhoan();
        }

        protected void gvTaiKhoan_RowDeleting(object sender, GridViewDeleteEventArgs e)
        {
            //  xóa user
            if (gvTaiKhoan.DataKeys != null)
            {
                string maTaiKhoan = gvTaiKhoan.DataKeys[e.RowIndex].Value.ToString();
                bool result = TaiKhoanBUS.DeleteTaiKhoanofMa(maTaiKhoan);
                if(!result)
                {
                    bool res = TaiKhoanBUS.UpdateTaiKhoanOfTrangThai(maTaiKhoan,"Lock");
                    if(res)
                    {
                        FillTaiKhoan();
                    }
                }
            }
            // chưa làm
        }

        protected void gvTaiKhoan_RowUpdating(object sender, GridViewUpdateEventArgs e)
        {
            if (e.NewValues == e.OldValues)
                e.Cancel = true;
            else
            {
                GridView gv = gvTaiKhoan;
                TextBox txTenTaiKhoan = (TextBox)gv.Rows[gv.EditIndex].Cells[1].Controls[0];
                DropDownList ddlLoaiTk = (DropDownList)gv.Rows[gv.EditIndex].FindControl("dllLoaiTaiKhoan");
                DropDownList ddlTinhTrang = (DropDownList)gv.Rows[gv.EditIndex].FindControl("ddlTrangThai");
                TextBox txtBan = (TextBox)gv.Rows[gv.EditIndex].Cells[4].Controls[0];
                TextBox txtMua = (TextBox)gv.Rows[gv.EditIndex].Cells[5].Controls[0];

                // chưa làm
                if (gv.DataKeys != null)
                {
                    string mataikhoan = gv.DataKeys[gv.EditIndex].Value.ToString();
                    TaiKhoanDTO taiKhoanDto = new TaiKhoanDTO();
                    taiKhoanDto.MaTaiKhoan = mataikhoan.Trim();
                    taiKhoanDto.TenTaiKhoan = txTenTaiKhoan.Text.Trim();
                    taiKhoanDto.TrangThai = ddlTinhTrang.SelectedItem.Value.Trim();
                    taiKhoanDto.MaLoaiTK = ddlLoaiTk.SelectedItem.Value.Trim();
                    taiKhoanDto.DiemTinCayBan = txtBan.Text.Trim();
                    taiKhoanDto.DiemTinCayMua = txtMua.Text.Trim();
               
                    bool result = TaiKhoanBUS.UpdateTaiKhoan(taiKhoanDto);
                }
            }
            gvTaiKhoan.EditIndex = -1;
            FillTaiKhoan();


        }

        protected void gvTaiKhoan_RowUpdated(object sender, GridViewUpdatedEventArgs e)
        {
            gvTaiKhoan.EditIndex = -1;
            FillTaiKhoan();

        }

        protected void gvTaiKhoan_RowEditing(object sender, GridViewEditEventArgs e)
        {

            //Giữ lại giá trị cũ của cột Loai
            String tenLoai = ((Label)gvTaiKhoan.Rows[e.NewEditIndex].Cells[3].FindControl("lblLoai")).Text;

            //Giữ lại giá trị cũ của cột Loai
            String trangThai = ((Label)gvTaiKhoan.Rows[e.NewEditIndex].Cells[4].FindControl("lblTrangThai")).Text;

            // Chuyen sang che do Edit
            gvTaiKhoan.EditIndex = e.NewEditIndex;
            FillTaiKhoan();



            //Thực hiện đỗ dữ liệu vào DropDownList loai
            DropDownList dllLoaiTaiKhoan = (DropDownList)gvTaiKhoan.Rows[e.NewEditIndex].FindControl("dllLoaiTaiKhoan");
            DataTable dt = LoaiTaiKhoanBUS.SelectAllLoai();
            dllLoaiTaiKhoan.DataSource = dt;
            dllLoaiTaiKhoan.DataTextField = "TenLoaiTK";
            dllLoaiTaiKhoan.DataValueField = "MaLoaiTK";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                if (dt.Rows[i]["TenLoaiTK"].ToString() == tenLoai)
                {
                    dllLoaiTaiKhoan.SelectedIndex = i;
                    break;
                }
            }
            dllLoaiTaiKhoan.DataBind();

            //Thực hiện đỗ dữ liệu vào DropDownList loai
            DropDownList ddlTrangThais = (DropDownList)gvTaiKhoan.Rows[e.NewEditIndex].FindControl("ddlTrangThai");
            ddlTrangThais.Items.Add("Unlock");
            ddlTrangThais.Items.Add("Lock");

            for (int i = 0; i < ddlTrangThais.Items.Count; i++)
            {
                if (ddlTrangThais.Items[i].Value.ToString() == trangThai)
                {
                    ddlTrangThais.SelectedIndex = i;
                    break;
                }
            }
            ddlTrangThais.DataBind();

        }

        protected void gvTaiKhoan_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
        {
            gvTaiKhoan.EditIndex = -1;
            FillTaiKhoan();
        }

        protected void gvTaiKhoan_SelectedIndexChanged(object sender, EventArgs e)
        {

        }

    }
}